//
//  UIPickerViewController.swift
//  swift-practice
//
//  Created by 余振涛 on 2021/7/23.
//

import UIKit

class UIPickerViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

    lazy var dataArray : NSMutableArray = {
        let array = NSMutableArray.init()
        return array
    }()

    init(withTitle title:String){
        super.init(nibName: nil, bundle: nil)
        self.title = title
        self.view.backgroundColor = .white
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createPickerView()
        // Do any additional setup after loading the view.
    }
    
    func createPickerView() {
        let pickerView = UIPickerView.init(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - 64))
        dataArray = [["1", "2", "3"],["1", "2", "3"],["1", "2", "3"],["1", "2", "3"],["1", "2", "3"]]
        pickerView.dataSource = self
        pickerView.delegate = self
        self.view.addSubview(pickerView)
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return (dataArray[component] as! NSArray?)?.count ?? 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return (dataArray[component] as! NSArray?)?[row] as? String
    }
    
}
